package com.redare.dev.smart.generator.service.dao;

import com.redare.dev.smart.generator.sdk.pojo.OnlFormField;
import com.redare.dev.smart.generator.sdk.pojo.form.OnlFormFieldForm;
import com.redare.devframework.common.pojo.Page;
import com.redare.devframework.sql.Sql;
import com.redare.devframework.sql.db.base.SpringJdbcHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * DAO
 *
 * @date 2021-08-28 23:03:12
 * Created by yangqiangyu
 */
@Repository(value = "com.redare.dev.smart.generator.dao.OnlFormFieldDao")
public class OnlFormFieldDao {

    @Autowired
    SpringJdbcHelper jdbcHelper;


    public long addOnlFormField(OnlFormFieldForm.Add form) {
        Sql sql = Sql.insert("onl_form_field")
                .field("cgformHeadId", form.getFormHeadId())
                .field("dbFieldName", form.getDbFieldName())
                .field("dbFieldTxt", form.getDbFieldTxt())
                .field("dbFieldNameOld", form.getDbFieldNameOld())
                .field("dbIsKey", form.getDbIsKey())
                .field("dbIsNull", form.getDbIsNull())
                .field("dbType", form.getDbType())
                .field("dbLength", form.getDbLength())
                .field("dbPointLength", form.getDbPointLength())
                .field("dbDefaultVal", form.getDbDefaultVal())
                .field("dictField", form.getDictField())
                .field("dictTable", form.getDictTable())
                .field("dictText", form.getDictText())
                .field("fieldShowType", form.getFieldShowType())
                .field("fieldHref", form.getFieldHref())
                .field("fieldLength", form.getFieldLength())
                .field("fieldValidType", form.getFieldValidType())
                .field("fieldMustInput", form.getFieldMustInput())
                .field("fieldExtendJson", form.getFieldExtendJson())
                .field("fieldDefaultValue", form.getFieldDefaultValue())
                .field("isQuery", form.getIsQuery())
                .field("isShowForm", form.getIsShowForm())
                .field("isShowList", form.getIsShowList())
                .field("isReadOnly", form.getIsReadOnly())
                .field("queryMode", form.getQueryMode())
                .field("mainTable", form.getMainTable())
                .field("mainField", form.getMainField())
                .field("orderNum", form.getOrderNum())
                .field("updateBy", form.getUpdateBy())
                .field("updateTime", form.getUpdateTime())
                .field("createTime", form.getCreateTime())
                .field("createBy", form.getCreateBy())
                .field("converter", form.getConverter())
                .field("queryDefVal", form.getQueryDefVal())
                .field("queryDictText", form.getQueryDictText())
                .field("queryDictField", form.getQueryDictField())
                .field("queryDictTable", form.getQueryDictTable())
                .field("queryShowType", form.getQueryShowType())
                .field("queryConfigFlag", form.getQueryConfigFlag())
                .field("queryValidType", form.getQueryValidType())
                .field("queryMustInput", form.getQueryMustInput())
                .field("sortFlag", form.getSortFlag())
                .field("createTime", new Date());
        return jdbcHelper.insertReturnFieldNumber(sql).longValue();
    }

    public int[] addOnlFormField(List<OnlFormFieldForm.Add> list) {
        if (list == null || list.isEmpty()) {
            return new int[]{};
        }
        Sql sql = Sql.insert("onl_form_field")
                .fields("formHeadId", "dbFieldName", "dbFieldTxt", "dbFieldNameOld", "dbIsKey", "dbIsNull", "dbType", "dbLength", "dbPointLength", "dbDefaultVal", "dictField", "dictTable", "dictText", "fieldShowType", "fieldHref", "fieldLength", "fieldValidType", "fieldMustInput", "fieldExtendJson", "fieldDefaultValue", "isQuery", "isShowForm", "isShowList", "isReadOnly", "queryMode", "mainTable", "mainField", "orderNum", "updateBy", "updateTime", "createTime", "createBy", "converter", "queryDefVal", "queryDictText", "queryDictField", "queryDictTable", "queryShowType", "queryConfigFlag", "queryValidType", "queryMustInput", "sortFlag", "createTime");
        Date createTime = new Date();
        List<Object[]> params = new ArrayList<>();
        for (OnlFormFieldForm.Add item : list) {
            params.add(new Object[]{item.getFormHeadId(), item.getDbFieldName(), item.getDbFieldTxt(), item.getDbFieldNameOld(), item.getDbIsKey(), item.getDbIsNull(), item.getDbType(), item.getDbLength(), item.getDbPointLength(), item.getDbDefaultVal(), item.getDictField(), item.getDictTable(), item.getDictText(), item.getFieldShowType(), item.getFieldHref(), item.getFieldLength(), item.getFieldValidType(), item.getFieldMustInput(), item.getFieldExtendJson(), item.getFieldDefaultValue(), item.getIsQuery(), item.getIsShowForm(), item.getIsShowList(), item.getIsReadOnly(), item.getQueryMode(), item.getMainTable(), item.getMainField(), item.getOrderNum(), item.getUpdateBy(), item.getUpdateTime(), item.getCreateTime(), item.getCreateBy(), item.getConverter(), item.getQueryDefVal(), item.getQueryDictText(), item.getQueryDictField(), item.getQueryDictTable(), item.getQueryShowType(), item.getQueryConfigFlag(), item.getQueryValidType(), item.getQueryMustInput(), item.getSortFlag(), createTime});
        }
        return jdbcHelper.batchUpdate(sql, params);
    }


    public int[] addOnlFormFieldByBean(List<OnlFormField> list) {
        if (list == null || list.isEmpty()) {
            return new int[]{};
        }
        Sql sql = Sql.insert("onl_form_field")
                .fields("form_head_id", "db_field_name", "comment", "db_field_name_old", "db_is_key", "db_is_null", "db_type","field_type","db_length", "db_point_length", "db_default_val", "dict_field", "dict_table","dict_value", "dict_text", "field_show_type", "field_href", "field_length", "field_valid_type", "field_must_input", "field_extend_json", "field_default_value", "query", "show_form", "show_list", "show_info","read_only", "query_mode", "main_table", "main_field", "order_num", "update_by", "update_time", "create_time", "create_by", "converter", "query_def_val", "query_dict_text", "query_dict_field", "query_dict_table", "query_show_type", "query_config_flag", "query_valid_type", "query_must_input", "sort_flag");
        Date createTime = new Date();
        List<Object[]> params = new ArrayList<>();
        for (OnlFormField item : list) {
            params.add(new Object[]{item.getFormHeadId(), item.getDbFieldName(), item.getComment(), item.getDbFieldName(), item.isDbIsKey(), item.isDbIsNull(), item.getDbType(), item.getFieldType(),item.getDbLength(), item.getDbPointLength(), item.getDbDefaultVal(), item.getDictField(), item.getDictTable(),item.getDictValue(), item.getDictText(), item.getFieldShowType(), item.getFieldHref(), item.getFieldLength(), item.getFieldValidType(), item.isFieldMustInput(), item.getFieldExtendJson(), item.getFieldDefaultValue(), item.isQuery(), item.isShowForm(), item.isShowList(),item.isShowInfo(), item.isReadOnly(), item.getQueryMode(), item.getMainTable(), item.getMainField(), item.getOrderNum(), item.getUpdateBy(), item.getUpdateTime(), createTime, item.getCreateBy(), item.getConverter(), item.getQueryDefVal(), item.getQueryDictText(), item.getQueryDictField(), item.getQueryDictTable(), item.getQueryShowType(), item.getQueryConfigFlag(), item.getQueryValidType(), item.isQueryMustInput(), item.isSortFlag()});
        }
        return jdbcHelper.batchUpdate(sql, params);
    }

    public int deleteOnlFormField(OnlFormFieldForm.Del form) {
        Sql sql = Sql.delete("onl_form_field")
                .where().andEq("id", form.getId());
        return jdbcHelper.update(sql.toString(), sql.getParams().toArray());
    }

    public Page<OnlFormField> findOnlFormFieldPage(OnlFormFieldForm.Search form, int curPage, int pageSize, boolean calCount, boolean allList) {
        Sql sql = Sql.select()
                .fieldsIf(StringUtils.isNotBlank(form.getQueryFields()), form.getQueryFields())
                .fieldsIf(StringUtils.isBlank(form.getQueryFields()), "*")
                .from("onl_form_field")
                .where()
                .andBLikeFieldsJoinByOrIfNotBlank(form.getKey()
                        , "db_field_name"
                )
                .andEqIfNotNull("form_head_id", form.getFormHeadId())
                .orderBy().asc("order_num").asc("id");
        return jdbcHelper.queryForPageBean(sql.toString(), OnlFormField.class, sql.getParams().toArray(), curPage, pageSize, calCount, allList);
    }

    public int editOnlFormField(OnlFormFieldForm.Edit form) {
        Sql sql = Sql.update("onl_form_field")
                .setIfNotNull("cgformHeadId", form.getFormHeadId())
                .setIfNotBlank("dbFieldName", form.getDbFieldName())
                .setIfNotBlank("dbFieldTxt", form.getDbFieldTxt())
                .setIfNotBlank("dbFieldNameOld", form.getDbFieldNameOld())
                .setIfNotNull("dbIsKey", form.getDbIsKey())
                .setIfNotNull("dbIsNull", form.getDbIsNull())
                .setIfNotBlank("dbType", form.getDbType())
                .setIfNotNull("dbLength", form.getDbLength())
                .setIfNotNull("dbPointLength", form.getDbPointLength())
                .setIfNotBlank("dbDefaultVal", form.getDbDefaultVal())
                .setIfNotBlank("dictField", form.getDictField())
                .setIfNotBlank("dictTable", form.getDictTable())
                .setIfNotBlank("dictText", form.getDictText())
                .setIfNotBlank("fieldShowType", form.getFieldShowType())
                .setIfNotBlank("fieldHref", form.getFieldHref())
                .setIfNotNull("fieldLength", form.getFieldLength())
                .setIfNotBlank("fieldValidType", form.getFieldValidType())
                .setIfNotBlank("fieldMustInput", form.getFieldMustInput())
                .setIfNotBlank("fieldExtendJson", form.getFieldExtendJson())
                .setIfNotBlank("fieldDefaultValue", form.getFieldDefaultValue())
                .setIfNotNull("query", form.getIsQuery())
                .setIfNotNull("isShowForm", form.getIsShowForm())
                .setIfNotNull("isShowList", form.getIsShowList())
                .setIfNotNull("isReadOnly", form.getIsReadOnly())
                .setIfNotBlank("queryMode", form.getQueryMode())
                .setIfNotBlank("mainTable", form.getMainTable())
                .setIfNotBlank("mainField", form.getMainField())
                .setIfNotNull("orderNum", form.getOrderNum())
                .setIfNotBlank("updateBy", form.getUpdateBy())
                .setIfNotNull("updateTime", form.getUpdateTime())
                .setIfNotNull("createTime", form.getCreateTime())
                .setIfNotBlank("createBy", form.getCreateBy())
                .setIfNotBlank("converter", form.getConverter())
                .setIfNotBlank("queryDefVal", form.getQueryDefVal())
                .setIfNotBlank("queryDictText", form.getQueryDictText())
                .setIfNotBlank("queryDictField", form.getQueryDictField())
                .setIfNotBlank("queryDictTable", form.getQueryDictTable())
                .setIfNotBlank("queryShowType", form.getQueryShowType())
                .setIfNotBlank("queryConfigFlag", form.getQueryConfigFlag())
                .setIfNotBlank("queryValidType", form.getQueryValidType())
                .setIfNotBlank("queryMustInput", form.getQueryMustInput())
                .setIfNotBlank("sortFlag", form.getSortFlag())
                .where()
                .andEq("id", form.getId());
        return jdbcHelper.update(sql.toString(), sql.getParams().toArray());
    }


    public int editNormalOnlFormFieldByBean(OnlFormField form) {
        Sql sql = Sql.update("onl_form_field")
                .setIfNotNull("form_head_id", form.getFormHeadId())
                .setIfNotBlank("db_field_name", form.getDbFieldName())
                .setIfNotBlank("comment", form.getComment())
                .setIfNotBlank("db_field_name_old", form.getDbFieldNameOld())
                .setIfNotNull("db_is_null", form.isDbIsNull())
                .setIfNotBlank("db_type", form.getDbType())
                .setIfNotBlank("field_type", form.getFieldType())
                .setIfNotNull("db_length", form.getDbLength())
                .setIfNotNull("db_point_length", form.getDbPointLength())
                .setIfNotNull("update_Time", form.getUpdateTime())
                .setIfNotNull("order_num",form.getOrderNum())
                .where()
                .andEq("id", form.getId());
        return jdbcHelper.update(sql.toString(), sql.getParams().toArray());
    }


    public int editOnlFormFieldByBean(OnlFormField form) {
        Sql sql = Sql.update("onl_form_field")
                .setIfNotNull("form_head_id", form.getFormHeadId())
                .setIfNotBlank("db_field_name", form.getDbFieldName())
                .setIfNotBlank("comment", form.getComment())
                .setIfNotBlank("db_field_name_old", form.getDbFieldNameOld())
                .setIfNotNull("db_is_key", form.isDbIsKey())
                .setIfNotNull("db_is_null", form.isDbIsNull())
                .setIfNotBlank("db_type", form.getDbType())
                .setIfNotBlank("field_type", form.getFieldType())
                .setIfNotNull("db_length", form.getDbLength())
                .setIfNotNull("db_point_length", form.getDbPointLength())
                .setIfNotBlank("db_default_val", form.getDbDefaultVal())
                .setIfNotBlank("dict_field", form.getDictField())
                .setIfNotBlank("dict_table", form.getDictTable())
                .setIfNotBlank("dict_text", form.getDictText())
                .setIfNotBlank("dict_value", form.getDictValue())
                .setIfNotBlank("dict_text_assign_field", form.getDictTextAssignField())
                .setIfNotBlank("field_show_type", form.getFieldShowType())
                .setIfNotBlank("field_href", form.getFieldHref())
                .setIfNotNull("field_length", form.getFieldLength())
                .setIfNotBlank("field_valid_type", form.getFieldValidType())
                .setIfNotNull("field_must_input", form.isFieldMustInput())
                .setIfNotBlank("field_extend_json", form.getFieldExtendJson())
                .setIfNotBlank("field_default_value", form.getFieldDefaultValue())
                .setIfNotNull("query", form.isQuery())
                .setIfNotNull("show_form", form.isShowForm())
                .setIfNotNull("show_list", form.isShowList())
                .setIfNotNull("show_info", form.isShowInfo())
                .setIfNotNull("read_only", form.isReadOnly())
                .setIfNotBlank("query_mode", form.getQueryMode())
                .setIfNotBlank("main_table", form.getMainTable())
                .setIfNotBlank("main_field", form.getMainField())
                .setIfNotNull("order_num", form.getOrderNum())
                .setIfNotBlank("update_By", form.getUpdateBy())
                .setIfNotNull("update_Time", form.getUpdateTime())
                .setIfNotNull("create_Time", form.getCreateTime())
                .setIfNotBlank("create_by", form.getCreateBy())
                .setIfNotBlank("converter", form.getConverter())
                .setIfNotBlank("query_def_val", form.getQueryDefVal())
                .setIfNotBlank("query_dict_text", form.getQueryDictText())
                .setIfNotBlank("query_dict_field", form.getQueryDictField())
                .setIfNotBlank("query_dict_table", form.getQueryDictTable())
                .setIfNotBlank("query_show_type", form.getQueryShowType())
                .setIfNotBlank("query_config_flag", form.getQueryConfigFlag())
                .setIfNotBlank("query_valid_type", form.getQueryValidType())
                .setIfNotNull("query_must_input", form.isQueryMustInput())
                .setIfNotNull("sort_flag", form.isSortFlag())
                .where()
                .andEq("id", form.getId());
        return jdbcHelper.update(sql.toString(), sql.getParams().toArray());
    }

    public OnlFormField getOnlFormField(OnlFormFieldForm.Get form) {
        Sql sql = Sql.select()
                .fieldsIf(StringUtils.isNotBlank(form.getQueryFields()), form.getQueryFields())
                .fieldsIf(StringUtils.isBlank(form.getQueryFields()), "*")
                .from("onl_form_field")
                .where()
                .andEq("id", form.getId());
        return jdbcHelper.queryForBean(sql.toString(), OnlFormField.class, sql.getParams().toArray());
    }


}

